﻿#include "ScriptInstructionFactory.h"

#include "InstructionDataReaderStartCollection.h"
#include "InstructionDataReaderStopCollection.h"
#include "InstructionLoop.h"
#include "InstructionThermostatConnect.h"
#include "InstructionThermostatDisconnect.h"
#include "InstructionThermostatTargetTemperature.h"
#include "InstructionThermostatTemperatureRate.h"
#include "InstructionTurnTableCloseCircuit.h"
#include "InstructionTurnTableConnect.h"
#include "InstructionTurnTableDisconnect.h"
#include "InstructionTurnTableEmergencyStop.h"
#include "InstructionTurnTableOpenCircuit.h"
#include "InstructionTurnTablePositionMode.h"
#include "InstructionTurnTablePowerOff.h"
#include "InstructionTurnTablePowerOn.h"
#include "InstructionTurnTableQuitConsole.h"
#include "InstructionTurnTableRateMode.h"
#include "InstructionTurnTableRun.h"
#include "InstructionTurnTableSetLoad.h"
#include "InstructionTurnTableStop.h"
#include "InstructionWait.h"
#include "ScriptInstruction.h"

const QString ScriptInstructionFactory::itemText_separator_head = QStringLiteral(",");
const QString ScriptInstructionFactory::itemText_separator_arg = QStringLiteral(":");
const QString ScriptInstructionFactory::itemText_wait = QStringLiteral("等待");
const QString ScriptInstructionFactory::itemText_wait_duration = QStringLiteral("时长（秒）");
const QString ScriptInstructionFactory::itemText_loop = QStringLiteral("循环");
const QString ScriptInstructionFactory::itemText_loop_index = QStringLiteral("编号");
const QString ScriptInstructionFactory::itemText_loop_count = QStringLiteral("次数");
const QString ScriptInstructionFactory::itemText_startCollection_dataReader = QStringLiteral("开始采集数据");
const QString ScriptInstructionFactory::itemText_stopCollection_dataReader = QStringLiteral("停止采集数据");
const QString ScriptInstructionFactory::itemText_connectToTurnTable = QStringLiteral("连接转台");
const QString ScriptInstructionFactory::itemText_disconnectFromTurnTable = QStringLiteral("断开转台连接");
const QString ScriptInstructionFactory::ItemText_setTurnTableLoaded = QStringLiteral("设置转台为负载模式");
const QString ScriptInstructionFactory::ItemText_setTurnTableUnloaded = QStringLiteral("设置转台为空载模式");
const QString ScriptInstructionFactory::itemText_powerOnTurnTable = QStringLiteral("上电");
const QString ScriptInstructionFactory::itemText_powerOffTurnTable = QStringLiteral("下电");
const QString ScriptInstructionFactory::itemText_closeTurnTableCircuit = QStringLiteral("闭合");
const QString ScriptInstructionFactory::itemText_openTurnTableCircuit = QStringLiteral("闲置");
const QString ScriptInstructionFactory::itemText_stopTurnTable = QStringLiteral("停止");
const QString ScriptInstructionFactory::itemText_emergencyStopTurnTable = QStringLiteral("急停");
const QString ScriptInstructionFactory::itemText_turnTableSearchZero = QStringLiteral("寻零");
const QString ScriptInstructionFactory::itemText_turnTableMoveToZero = QStringLiteral("回零");
const QString ScriptInstructionFactory::itemText_setTurnTablePositionMode = QStringLiteral("设置位置模式");
const QString ScriptInstructionFactory::itemText_setTurnTablePositionMode_position = QStringLiteral("位置（度）");
const QString ScriptInstructionFactory::itemText_setTurnTablePositionMode_rate = QStringLiteral("速率（度每秒）");
const QString ScriptInstructionFactory::itemText_setTurnTablePositionMode_acceleration = QStringLiteral("加速度（度每秒平方）");
const QString ScriptInstructionFactory::itemText_setTurnTableRateMode = QStringLiteral("设置速率模式");
const QString ScriptInstructionFactory::itemText_setTurnTableRateMode_rate = QStringLiteral("速率（度每秒）");
const QString ScriptInstructionFactory::itemText_setTurnTableRateMode_acceleration = QStringLiteral("加速度（度每秒平方）");
const QString ScriptInstructionFactory::itemText_turnTableRunPositionMode = QStringLiteral("运行位置模式");
const QString ScriptInstructionFactory::itemText_turnTableRunRateMode = QStringLiteral("运行速率模式");
const QString ScriptInstructionFactory::itemText_searchTurnTableCurrentState = QStringLiteral("查询现场状态");
const QString ScriptInstructionFactory::itemText_searchTurnTableSearchZeroState = QStringLiteral("查询寻零状态");
const QString ScriptInstructionFactory::itemText_searchTurnTableRunningState = QStringLiteral("查询运行状态");
const QString ScriptInstructionFactory::itemText_searchTurnTableRunningMode = QStringLiteral("查询运行模式");
const QString ScriptInstructionFactory::itemText_connectToThermostat = QStringLiteral("连接温箱");
const QString ScriptInstructionFactory::itemText_disconnectFromThermostat = QStringLiteral("断开温箱连接");
const QString ScriptInstructionFactory::itemText_setThermostatTargetTemperature = QStringLiteral("设置温箱目标温度");
const QString ScriptInstructionFactory::itemText_setThermostatTargetTemperature_temperature = QStringLiteral("温度（度）");
const QString ScriptInstructionFactory::itemText_searchThermostatCurrentTemperature = QStringLiteral("查询当前温度");
const QString ScriptInstructionFactory::itemText_setThermostatTemperatureRate = QStringLiteral("设置温箱温度速率");
const QString ScriptInstructionFactory::itemText_setThermostatTemperatureRate_rate = QStringLiteral("速率（度每秒）");
const QString ScriptInstructionFactory::itemText_quitTurnTableConsole = QStringLiteral("退出控制台");

ScriptInstructionFactory::ScriptInstructionFactory(QObject* parent) :QObject(parent)
{
}

ScriptInstructionFactory::~ScriptInstructionFactory()
{}

QVector<QVector<ScriptInstruction*>> ScriptInstructionFactory::floatListToScriptList(QVector<QVector<QVector<float>>> floatList, bool* isOk)//如果返回空的脚本则意味着解析失败
{
	QVector<QVector<ScriptInstruction*>> scriptList;
	for (int i = 0; i < floatList.size(); ++i)
	{
		QVector<ScriptInstruction*> scriptList1;
		for (int j = 0; j < floatList[i].size(); ++j)
		{
			ScriptInstruction* scriptInstruction = floatToScript(floatList[i][j]);
			if (scriptInstruction != Q_NULLPTR)
			{
				scriptList1.append(scriptInstruction);
			}
			else
			{
				for (QVector<ScriptInstruction*> temp1 : scriptList)
				{
					for (ScriptInstruction* temp2 : temp1)
					{
						temp2->deleteLater();
					}
				}
				*isOk = false;
				return QVector<QVector<ScriptInstruction*>>();
			}
		}
		scriptList.append(scriptList1);
	}
	*isOk = true;
	return scriptList;
}
QVector<QTreeWidgetItem*> ScriptInstructionFactory::floatListToTreeWidgetItemList(QVector<QVector<QVector<float>>> floatList, bool* isOk)
{
	QVector<QTreeWidgetItem*> treeWidgetItemList;
	for (int i = 0; i < floatList[0].size(); ++i)
	{
		QTreeWidgetItem* treeWidgetItem = floatToTreeWidgetItem(floatList[0][i], floatList);
		if (treeWidgetItem != Q_NULLPTR)
		{
			treeWidgetItemList.append(treeWidgetItem);
		}
		else
		{
			for (int j = 0; j < treeWidgetItemList.size(); ++j)
			{
				circularDelete(treeWidgetItemList.at(j));
			}
			*isOk = false;
			return QVector<QTreeWidgetItem*>();
		}
	}
	*isOk = true;
	return treeWidgetItemList;
}
QVector<QVector<QVector<float>>> ScriptInstructionFactory::scriptListToFloatList(QVector<QVector<ScriptInstruction*>> scriptList, bool* isOk)
{
	QVector<QVector<QVector<float>>> floatList;
	for (int i = 0; i < scriptList.size(); ++i)
	{
		QVector<QVector<float>> floatList1;
		for (int j = 0; j < scriptList[i].size(); ++j)
		{
			floatList1.append(scriptList[i][j]->getDataForSaving());
		}
		floatList.append(floatList1);
	}
	*isOk = true;
	return floatList;
}
QVector<QTreeWidgetItem*> ScriptInstructionFactory::scriptListToTreeWidgetItemList(QVector<QVector<ScriptInstruction*>> scriptList, bool* isOk)
{
	QVector<QVector<QVector<float>>> floatList = scriptListToFloatList(scriptList, isOk);
	if (*isOk)
	{
		return floatListToTreeWidgetItemList(floatList, isOk);
	}
	else
	{
		return QVector<QTreeWidgetItem*>();
	}
}
QVector<QVector<QVector<float>>> ScriptInstructionFactory::treeWidgetItemListToFloatList(QVector<QTreeWidgetItem*> treeWidgetItemList, bool* isOk)
{
	QVector<QVector<QVector<float>>> floatList;
	QVector<QTreeWidgetItem*> parentTreeWidgetItemList = getParentTreeWidgetItemList(treeWidgetItemList);
	QVector<QVector<float>> floatList1;
	for (int i = 0; i < treeWidgetItemList.size(); ++i)
	{
		QVector<float> temp = treeWidgetItemToFloat(treeWidgetItemList[i], parentTreeWidgetItemList);
		if (temp.isEmpty())
		{
			*isOk = false;
			return QVector<QVector<QVector<float>>>();
		}
		floatList1.append(temp);
	}
	floatList.append(floatList1);
	for (int i = 0; i < parentTreeWidgetItemList.size(); ++i)
	{
		floatList1.clear();
		for (int j = 0; j < parentTreeWidgetItemList[i]->childCount(); ++j)
		{
			QVector<float> temp = treeWidgetItemToFloat(parentTreeWidgetItemList[i]->child(j), parentTreeWidgetItemList);
			if (temp.isEmpty())
			{
				*isOk = false;
				return QVector<QVector<QVector<float>>>();
			}
			floatList1.append(temp);
		}
		floatList.append(floatList1);
	}
	*isOk = true;
	return floatList;
}
QVector<QVector<ScriptInstruction*>> ScriptInstructionFactory::treeWidgetItemListToScriptList(QVector<QTreeWidgetItem*> treeWidgetItemList, bool* isOk)
{
	QVector<QVector<QVector<float>>> floatList = treeWidgetItemListToFloatList(treeWidgetItemList, isOk);
	if (*isOk)
	{
		return floatListToScriptList(floatList, isOk);
	}
	else
	{
		return QVector<QVector<ScriptInstruction*>>();
	}
}
ScriptInstruction* ScriptInstructionFactory::floatToScript(QVector<float> scriptInstruction)
{
	switch ((int)(scriptInstruction.at(0)))
	{
	case ScriptInstruction::MetaInstruction_Wait:
		return new InstructionWait(scriptInstruction.at(1));
	case ScriptInstruction::MetaInstruction_Loop:
		return new InstructionLoop(scriptInstruction.at(1), scriptInstruction.at(2));
	case ScriptInstruction::Instruction_DataReader_StartCollection:
		return new InstructionDataReaderStartCollection();
	case ScriptInstruction::Instruction_DataReader_StopCollection:
		return new InstructionDataReaderStopCollection();
	case ScriptInstruction::Instruction_TurnTable_SetLoad:
		if (scriptInstruction.at(1) == TurnTableCodec::Mode_Unloaded)
		{
			return new InstructionTurnTableSetLoad(TurnTableCodec::Mode_Unloaded);
		}
		else if (scriptInstruction.at(1) == TurnTableCodec::Mode_Loaded)
		{
			return new InstructionTurnTableSetLoad(TurnTableCodec::Mode_Loaded);
		}
		else
		{
			return Q_NULLPTR;
		}
	case ScriptInstruction::Instruction_TurnTable_PowerOn:
		return new InstructionTurnTablePowerOn();
	case ScriptInstruction::Instruction_TurnTable_PowerOff:
		return new InstructionTurnTablePowerOff();
	case ScriptInstruction::Instruction_TurnTable_CloseCircuit:
		return new InstructionTurnTableCloseCircuit();
	case ScriptInstruction::Instruction_TurnTable_OpenCircuit:
		return new InstructionTurnTableOpenCircuit();
	case ScriptInstruction::Instruction_TurnTable_Stop:
		return new InstructionTurnTableStop();
	case ScriptInstruction::Instruction_TurnTable_EmergencyStop:
		return new InstructionTurnTableEmergencyStop();
	case ScriptInstruction::Instruction_TurnTable_PositionMode:
		return new InstructionTurnTablePositionMode(scriptInstruction.at(1), scriptInstruction.at(2), scriptInstruction.at(3));
	case ScriptInstruction::Instruction_TurnTable_RateMode:
		return new InstructionTurnTableRateMode(scriptInstruction.at(1), scriptInstruction.at(2));
	case ScriptInstruction::Instruction_TurnTable_Run:
		if (scriptInstruction.at(1))
		{
			return new InstructionTurnTableRun(TurnTableCodec::Mode_PositionMode);
		}
		else if (scriptInstruction.at(2))
		{
			return new InstructionTurnTableRun(TurnTableCodec::Mode_RateMode);
		}
		else
		{
			return Q_NULLPTR;
		}
	case ScriptInstruction::Instruction_Thermostat_TargetTemperature:
		return new InstructionThermostatTargetTemperature(scriptInstruction.at(1));
	case ScriptInstruction::Instruction_Thermostat_TemperatureRate:
		return new InstructionThermostatTemperatureRate(scriptInstruction.at(1));
	default:
		return Q_NULLPTR;
	}
}
QTreeWidgetItem* ScriptInstructionFactory::floatToTreeWidgetItem(QVector<float> scriptInstruction, QVector<QVector<QVector<float>>> floatList)
{
	QTreeWidgetItem* treeWidgetItem = new QTreeWidgetItem();
	switch ((int)(scriptInstruction.at(0)))
	{
	case ScriptInstruction::MetaInstruction_Wait:
		treeWidgetItem->setText(0, getDescribe_Wait(scriptInstruction.at(1)));
		break;
	case ScriptInstruction::MetaInstruction_Loop:
		treeWidgetItem->setText(0, getDescribe_Loop(scriptInstruction.at(2)));
		for (int i = 0; i < floatList[(int)scriptInstruction.at(1)].size(); ++i)
		{//遍历每个指令
			QTreeWidgetItem* temp = floatToTreeWidgetItem(floatList[(int)scriptInstruction.at(1)][i], floatList);
			if (temp != Q_NULLPTR)
			{
				treeWidgetItem->addChild(temp);//添加到顶层节点
			}
			else
			{
				circularDelete(treeWidgetItem);
				treeWidgetItem = Q_NULLPTR;
				break;
			}
		}
		break;
	case ScriptInstruction::Instruction_DataReader_StartCollection:
		treeWidgetItem->setText(0, itemText_startCollection_dataReader);
		break;
	case ScriptInstruction::Instruction_DataReader_StopCollection:
		treeWidgetItem->setText(0, itemText_stopCollection_dataReader);
		break;
	case ScriptInstruction::Instruction_TurnTable_SetLoad:
		if (scriptInstruction.at(1) == TurnTableCodec::Mode_Unloaded)
		{
			treeWidgetItem->setText(0, ItemText_setTurnTableUnloaded);
		}
		else if (scriptInstruction.at(1) == TurnTableCodec::Mode_Loaded)
		{
			treeWidgetItem->setText(0, ItemText_setTurnTableLoaded);
		}
		else
		{
			delete treeWidgetItem;
			treeWidgetItem = Q_NULLPTR;
		}
		break;
	case ScriptInstruction::Instruction_TurnTable_PowerOn:
		treeWidgetItem->setText(0, itemText_powerOnTurnTable);
		break;
	case ScriptInstruction::Instruction_TurnTable_PowerOff:
		treeWidgetItem->setText(0, itemText_powerOffTurnTable);
		break;
	case ScriptInstruction::Instruction_TurnTable_CloseCircuit:
		treeWidgetItem->setText(0, itemText_closeTurnTableCircuit);
		break;
	case ScriptInstruction::Instruction_TurnTable_OpenCircuit:
		treeWidgetItem->setText(0, itemText_openTurnTableCircuit);
		break;
	case ScriptInstruction::Instruction_TurnTable_Stop:
		treeWidgetItem->setText(0, itemText_stopTurnTable);
		break;
	case ScriptInstruction::Instruction_TurnTable_EmergencyStop:
		treeWidgetItem->setText(0, itemText_emergencyStopTurnTable);
		break;
	case ScriptInstruction::Instruction_TurnTable_PositionMode:
		treeWidgetItem->setText(0, getDescribe_TurnTable_PositionMode(scriptInstruction.at(1), scriptInstruction.at(2), scriptInstruction.at(3)));
		break;
	case ScriptInstruction::Instruction_TurnTable_RateMode:
		treeWidgetItem->setText(0, getDescribe_TurnTable_RateMode(scriptInstruction.at(1), scriptInstruction.at(2)));
		break;
	case ScriptInstruction::Instruction_TurnTable_Run:
		if (scriptInstruction.at(1) == TurnTableCodec::Mode_PositionMode)
		{
			treeWidgetItem->setText(0, itemText_turnTableRunPositionMode);
		}
		else if (scriptInstruction.at(1) == TurnTableCodec::Mode_PositionMode)
		{
			treeWidgetItem->setText(0, itemText_turnTableRunRateMode);
		}
		else
		{
			delete treeWidgetItem;
			treeWidgetItem = Q_NULLPTR;
		}
		break;
	case ScriptInstruction::Instruction_Thermostat_TargetTemperature:
		treeWidgetItem->setText(0, getDescribe_Thermostat_TargetTemperature(scriptInstruction.at(1)));
		break;
	case ScriptInstruction::Instruction_Thermostat_TemperatureRate:
		treeWidgetItem->setText(0, getDescribe_Thermostat_TemperatureRate(scriptInstruction.at(1)));
		break;
	default:
		delete treeWidgetItem;
		treeWidgetItem = Q_NULLPTR;
		break;
	}
	return treeWidgetItem;
}

QVector<float> ScriptInstructionFactory::treeWidgetItemToFloat(QTreeWidgetItem* scriptInstruction, QVector<QTreeWidgetItem*> parentTreeWidgetItemList)
{
	QString scriptDescribe = scriptInstruction->text(0);
	QString describeHead = getDescribeHead(scriptDescribe);
	QVector<float> describeArgList;
	if (describeHead == itemText_wait) {
		describeArgList = getDescribeArgList(scriptDescribe);
		describeArgList.prepend(ScriptInstruction::MetaInstruction_Wait);
		return describeArgList;
	}
	if (describeHead == itemText_loop) {
		describeArgList = getDescribeArgList(scriptDescribe);
		int parentTreeWidgetItemIndex = parentTreeWidgetItemList.indexOf(scriptInstruction);
		if (parentTreeWidgetItemIndex != -1)
		{
			describeArgList.prepend(parentTreeWidgetItemIndex + 1);
			describeArgList.prepend(ScriptInstruction::MetaInstruction_Loop);
			return describeArgList;
		}
		else
		{
			return QVector<float>();
		}
	}
	if (describeHead == itemText_startCollection_dataReader) {
		describeArgList.prepend(ScriptInstruction::Instruction_DataReader_StartCollection);
		return describeArgList;
	}
	if (describeHead == itemText_stopCollection_dataReader) {
		describeArgList.prepend(ScriptInstruction::Instruction_DataReader_StopCollection);
		return describeArgList;
	}
	if (describeHead == ItemText_setTurnTableLoaded) {
		describeArgList.append(ScriptInstruction::Instruction_TurnTable_SetLoad);
		describeArgList.append(TurnTableCodec::Mode_Loaded);
		return describeArgList;
	}
	if (describeHead == ItemText_setTurnTableUnloaded) {
		describeArgList.append(ScriptInstruction::Instruction_TurnTable_SetLoad);
		describeArgList.append(TurnTableCodec::Mode_Unloaded);
		return describeArgList;
	}
	if (describeHead == itemText_powerOnTurnTable) {
		describeArgList.append(ScriptInstruction::Instruction_TurnTable_PowerOn);
		return describeArgList;
	}
	if (describeHead == itemText_powerOffTurnTable) {
		describeArgList.append(ScriptInstruction::Instruction_TurnTable_PowerOff);
		return describeArgList;
	}
	if (describeHead == itemText_closeTurnTableCircuit) {
		describeArgList.append(ScriptInstruction::Instruction_TurnTable_CloseCircuit);
		return describeArgList;
	}
	if (describeHead == itemText_openTurnTableCircuit) {
		describeArgList.append(ScriptInstruction::Instruction_TurnTable_OpenCircuit);
		return describeArgList;
	}
	if (describeHead == itemText_stopTurnTable) {
		describeArgList.append(ScriptInstruction::Instruction_TurnTable_Stop);
		return describeArgList;
	}
	if (describeHead == itemText_emergencyStopTurnTable) {
		describeArgList.append(ScriptInstruction::Instruction_TurnTable_EmergencyStop);
		return describeArgList;
	}
	if (describeHead == itemText_setTurnTablePositionMode) {
		describeArgList = getDescribeArgList(scriptDescribe);
		describeArgList.prepend(ScriptInstruction::Instruction_TurnTable_PositionMode);
		return describeArgList;
	}
	if (describeHead == itemText_setTurnTableRateMode) {
		describeArgList = getDescribeArgList(scriptDescribe);
		describeArgList.prepend(ScriptInstruction::Instruction_TurnTable_RateMode);
		return describeArgList;
	}
	if (describeHead == itemText_turnTableRunPositionMode) {
		describeArgList.append(ScriptInstruction::Instruction_TurnTable_Run);
		describeArgList.append(TurnTableCodec::Mode_PositionMode);
		return describeArgList;
	}
	if (describeHead == itemText_turnTableRunRateMode) {
		describeArgList.append(ScriptInstruction::Instruction_TurnTable_Run);
		describeArgList.append(TurnTableCodec::Mode_RateMode);
		return describeArgList;
	}
	if (describeHead == itemText_setThermostatTargetTemperature) {
		describeArgList = getDescribeArgList(scriptDescribe);
		describeArgList.prepend(ScriptInstruction::Instruction_Thermostat_TargetTemperature);
		return describeArgList;
	}
	if (describeHead == itemText_setThermostatTemperatureRate) {
		describeArgList = getDescribeArgList(scriptDescribe);
		describeArgList.prepend(ScriptInstruction::Instruction_Thermostat_TemperatureRate);
		return describeArgList;
	}
	return QVector<float>();
}

QString ScriptInstructionFactory::getDescribe_Wait(int milliseconds)
{
	return itemText_wait +
		itemText_separator_head +
		itemText_wait_duration +
		itemText_separator_arg +
		QString::number(milliseconds / 1000);
}
QString ScriptInstructionFactory::getDescribe_Loop(int scriptIndex, int loopCount)
{
	return itemText_loop +
		itemText_separator_head +
		itemText_loop_index +
		itemText_separator_arg +
		QString::number(scriptIndex) +
		itemText_separator_head +
		itemText_loop_count +
		itemText_separator_arg +
		QString::number(loopCount);
}
QString ScriptInstructionFactory::getDescribe_Loop(int loopCount)
{
	return itemText_loop +
		itemText_separator_head +
		itemText_loop_count +
		itemText_separator_arg +
		QString::number(loopCount);
}
QString ScriptInstructionFactory::getDescribe_DataReader_StartCollection()
{
	return itemText_startCollection_dataReader;
}
QString ScriptInstructionFactory::getDescribe_DataReader_StopCollection()
{
	return itemText_stopCollection_dataReader;
}
QString ScriptInstructionFactory::getDescribe_TurnTable_Connect()
{
	return itemText_connectToTurnTable;
}
QString ScriptInstructionFactory::getDescribe_TurnTable_Disconnect()
{
	return itemText_disconnectFromTurnTable;
}
QString ScriptInstructionFactory::getDescribe_TurnTable_SetLoad(TurnTableCodec::TurnTableLoadMode loadMode)
{
	switch (loadMode)
	{
	case TurnTableCodec::Mode_Unloaded:
		return ItemText_setTurnTableUnloaded;
	case TurnTableCodec::Mode_Loaded:
		return ItemText_setTurnTableLoaded;
	default:
		return "";
	}
}
QString ScriptInstructionFactory::getDescribe_TurnTable_PowerOn()
{
	return itemText_powerOnTurnTable;
}
QString ScriptInstructionFactory::getDescribe_TurnTable_PowerOff()
{
	return itemText_powerOffTurnTable;
}
QString ScriptInstructionFactory::getDescribe_TurnTable_CloseCircuit()
{
	return itemText_closeTurnTableCircuit;
}
QString ScriptInstructionFactory::getDescribe_TurnTable_OpenCircuit()
{
	return itemText_openTurnTableCircuit;
}
QString ScriptInstructionFactory::getDescribe_TurnTable_Stop()
{
	return itemText_stopTurnTable;
}
QString ScriptInstructionFactory::getDescribe_TurnTable_EmergencyStop()
{
	return itemText_emergencyStopTurnTable;
}
QString ScriptInstructionFactory::getDescribe_TurnTable_PositionMode(float position, float rate, float acceleration)
{
	return itemText_setTurnTablePositionMode +
		itemText_separator_head +
		itemText_setTurnTablePositionMode_position +
		itemText_separator_arg +
		QString::number(position, 'f', 4) +
		itemText_separator_head +
		itemText_setTurnTablePositionMode_rate +
		itemText_separator_arg +
		QString::number(rate, 'f', 4) +
		itemText_separator_head +
		itemText_setTurnTablePositionMode_acceleration +
		itemText_separator_arg +
		QString::number(acceleration, 'f', 4);
}
QString ScriptInstructionFactory::getDescribe_TurnTable_RateMode(float rate, float acceleration)
{
	return itemText_setTurnTableRateMode +
		itemText_separator_head +
		itemText_setTurnTableRateMode_rate +
		itemText_separator_arg +
		QString::number(rate, 'f', 4) +
		itemText_separator_head +
		itemText_setTurnTableRateMode_acceleration +
		itemText_separator_arg +
		QString::number(acceleration, 'f', 4);
}
QString ScriptInstructionFactory::getDescribe_TurnTable_Run(TurnTableCodec::TurnTableRunningMode runningMode)
{
	switch (runningMode)
	{
	case TurnTableCodec::Mode_PositionMode:
		return itemText_turnTableRunPositionMode;
	case TurnTableCodec::Mode_RateMode:
		return itemText_turnTableRunRateMode;
	default:
		return "";
	}
}
QString ScriptInstructionFactory::getDescribe_Thermostat_Connect()
{
	return itemText_connectToThermostat;;
}
QString ScriptInstructionFactory::getDescribe_Thermostat_Disconnect()
{
	return itemText_disconnectFromThermostat;;
}
QString ScriptInstructionFactory::getDescribe_Thermostat_TargetTemperature(float targetTemperature)
{
	return itemText_setThermostatTargetTemperature +
		itemText_separator_head +
		itemText_setThermostatTargetTemperature_temperature +
		itemText_separator_arg +
		QString::number(targetTemperature, 'f', 4);
}
QString ScriptInstructionFactory::getDescribe_Thermostat_TemperatureRate(float targetTempratureRate)
{
	return itemText_setThermostatTemperatureRate +
		itemText_separator_head +
		itemText_setThermostatTemperatureRate_rate +
		itemText_separator_arg +
		QString::number(targetTempratureRate, 'f', 4);
}

QString ScriptInstructionFactory::getDescribeHead(QString scriptDescribe)//得到treeitem分解后的头部，即指令
{
	return scriptDescribe.split(itemText_separator_head, QString::SkipEmptyParts).at(0);
}
QVector<float> ScriptInstructionFactory::getDescribeArgList(QString scriptDescribe)//得到treeitem分解后的参数列表
{
	QStringList itemTextList = scriptDescribe.split(itemText_separator_head, QString::SkipEmptyParts);
	QVector<float> argList;
	for (int i = 1; i < itemTextList.size(); ++i) {
		argList.append(itemTextList[i].split(itemText_separator_arg, QString::SkipEmptyParts)[1].toFloat());
	}
	return argList;
}
QVector<QTreeWidgetItem*> ScriptInstructionFactory::getParentTreeWidgetItemList(QVector<QTreeWidgetItem*> treeWidgetItemList)
{
	QVector<QTreeWidgetItem*> parentTreeWidgetItemList;
	for (int i = 0; i < treeWidgetItemList.size(); ++i)
	{
		if (getDescribeHead(treeWidgetItemList[i]->text(0)) == itemText_loop)
		{
			parentTreeWidgetItemList.append(treeWidgetItemList[i]);
			QVector<QTreeWidgetItem*> childTreeWidgetItemList;
			for (int j = 0; j < treeWidgetItemList[i]->childCount(); ++j)
			{
				childTreeWidgetItemList.append(treeWidgetItemList[i]->child(j));
			}
			if (!childTreeWidgetItemList.isEmpty())
			{
				parentTreeWidgetItemList.append(getParentTreeWidgetItemList(childTreeWidgetItemList));
			}
		}
	}
	return parentTreeWidgetItemList;
}

void ScriptInstructionFactory::circularDelete(QTreeWidgetItem* treeWidgetItem)
{
	for (int i = 0; i < treeWidgetItem->childCount(); ++i)
	{
		circularDelete(treeWidgetItem->child(i));
	}
	delete treeWidgetItem;
}
